package cn.bossx.pet.controller;


import cn.bossx.basic.util.AjaxResult;
import cn.bossx.basic.util.LoginContext;
import cn.bossx.basic.util.PageList;
import cn.bossx.pet.domain.Pet;
import cn.bossx.pet.query.PetQuery;
import cn.bossx.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 领养宠物
     * @param id 哪一个宠物
     * @return
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            petService.adopt(id, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }
    }

    //获取一条数据   GET
    @GetMapping("/{id}")
    public Pet findById(@PathVariable("id") Long id){
        return petService.loadByIdDetail(id);
    }

    //分页 + 高级查询   POST
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query){
        return petService.queryPage(query);
    }

    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (RuntimeException e) {//我们应该还有自己抛出的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {//系统级别的错误  我们应该还有自己抛出的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (RuntimeException e) {//我们应该还有自己抛出的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {//系统级别的错误  我们应该还有自己抛出的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }


}
